function Util() {};
//获取url参数函数
Util.prototype.getUrlParams = function(url) {
    url = url || location.href;
    var obj = {},
        reg = /([^?=&#]+)=([^?=&#]+)/g;

    url.replace(reg, function() {
        obj[arguments[1]] = decodeURIComponent(arguments[2]);
    });
    return obj;
};

//获得参数类型
Util.prototype.getType = function(target) {
    return Object.prototype.toString.call(target).replace(/^\[object ([a-zA-Z]+)\]$/, "$1").toLowerCase();
}

Util.prototype.updateUrl = function(url,key){
    var key= (key || 't') +'=';  //默认是"t"
    var reg=new RegExp(key+'\\d+');  //正则：t=1472286066028
    var timestamp=+new Date();
    if(url.indexOf(key)>-1){ //有时间戳，直接更新
        return url.replace(reg,key+timestamp);
    }else{  //没有时间戳，加上时间戳
        if(url.indexOf('\?')>-1){
            var urlArr=url.split('\?');
            if(urlArr[1]){
                return urlArr[0]+'?'+key+timestamp+'&'+urlArr[1];
            }else{
                return urlArr[0]+'?'+key+timestamp;
            }
        }else{
            if(url.indexOf('#')>-1){
                return url.split('#')[0]+'?'+key+timestamp+location.hash;
            }else{
                return url+'?'+key+timestamp;
            }
        }
    }
}

/*
 * 频率控制 返回函数连续调用时，fn 执行频率限定为每多少时间执行一次
 * @param fn {function}  需要调用的函数
 * @param delay  {number}    延迟时间，单位毫秒
 * @param immediate  {bool} 给 immediate参数传递false 绑定的函数先执行，而不是delay后后执行。
 * @return {function}	实际调用函数
 */
Util.prototype.throttle = function (fn,delay, immediate, debounce) {
    var curr = +new Date(),//当前时间
        last_call = 0,
        last_exec = 0,
        timer = null,
        diff, //时间差
        context,//上下文
        args,
        exec = function () {
            last_exec = curr;
            fn.apply(context, args);
        };
    return function () {
        curr= +new Date();
        context = this,
        args = arguments,
        diff = curr - (debounce ? last_call : last_exec) - delay;
        clearTimeout(timer);

        if (debounce) {
            if (immediate) {
                timer = setTimeout(exec, delay);
            } else if (diff >= 0) {
                exec();
            }
        } else {
            if (diff >= 0) {
                exec();
            } else if (immediate) {
                timer = setTimeout(exec, -diff);
            }
        }

        last_call = curr;
    }
};
/*
 * 空闲控制 返回函数连续调用时，空闲时间必须大于或等于 delay，fn 才会执行
 * @param fn {function}  要调用的函数
 * @param delay   {number}    空闲时间
 * @param immediate  {bool} 给 immediate参数传递false 绑定的函数先执行，而不是delay后后执行。
 * @return {function}	实际调用函数
 */
Util.prototype.debounce = function (fn, delay, immediate) {
    return this.throttle(fn, delay, immediate, true);
};

Util.prototype.toFixed = function(num, s) {
    var that = num,changenum,index;
    // 负数
    if(num < 0){
        that = -1 * that;
    }

    changenum = (parseInt(that * Math.pow(10, s) + 0.5) / Math.pow(10, s)).toString();
    index = changenum.indexOf(".");
    if(index < 0 && s > 0) {
        changenum = changenum + ".";
        for (var i = 0; i < s; i++) {
            changenum = changenum + "0";
        }
    }else{
        index = changenum.length - index;
        for (var i = 0; i < (s - index) + 1; i++) {
            changenum = changenum + "0";
        }
    }

    if(num<0){
        return "-" + changenum;
    }else{
        return changenum;
    }
}


Util.prototype.getUid = function (ele) {
    return Math.random().toString(36).slice(2);
}

let player = {
    percent:function (num1,num2) {
        if(num2 === 0) return "0%";
        let percent = (num1 / num2);
        return Math.min(percent,1) * 100 + "%"
    },
    duration:function (ss,format) {
        let mm = Math.floor(ss/60); //分钟
        ss = Math.floor(ss - mm * 60);
        mm = this.addZero(mm);
        ss = this.addZero(ss) ;
        if(format){
            return format.replace(/mm/,mm).replace(/ss/,ss);
        }
        return `${mm}:${ss}`
    },
    addZero:function (num){
        return num < 10 ? `0${num}` : num
    }
}

Util.prototype.audio = {
    ...player
}

Util.prototype.storage = {
    set:function (key,value) {
        if(!localStorage){
            return
        }
        if(!value){
            return
        }
        localStorage.setItem(key,JSON.stringify(value))
    },
    get:function (key) {
        if(!localStorage){
            return
        }
        let value = localStorage.getItem(key);
        if(!value) return;
        return JSON.parse(value)
    },
    del:function (key){
        if(!localStorage){
            return
        }
        localStorage.removeItem(key)
    },
    clear:function () {
        localStorage.clear()
    }
}

Util.prototype.wxpay = function (config) {

    return new Promise((resolve,reject) => {
        function onBridgeReady() {
            WeixinJSBridge.invoke('getBrandWCPayRequest',config,
            function(res) {
                if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                   resolve("ok")
                } else if(res.err_msg != "get_brand_wcpay_request:cancel" ){
                    resolve("cancel")
                }else{
                    resolve("faild")
                }
            });
        }
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady();
        }
    })
    
}


window.zrzb = new Util(); 